package com.wesley.arrays;

public class ArrayDemo04 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4};

        //JDK 1.5 没有下标
        for (int array : arrays) {
            System.out.println(array);
        }
        System.out.println("======");
        //数组作为方法入参
        printArray(arrays);

        System.out.println();
        System.out.println("======");
        //数组作为返回值
        //方法有返回值时，就需要进行赋左值
        //变量名 可与 方法名 一样
        int[] reverse = reverse(arrays);
        printArray(reverse);

    }

    //打印数组元素
    public static void printArray(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }
    }

    //反转数组
    public static int[] reverse(int[] arrays){
        int[] result = new int[arrays.length];

        //反转操作
        for (int i = 0, j = result.length - 1; i < arrays.length; i++,j--) {
            result[j] = arrays[i];
        }

        return result;
    }
}
